package com.xinzhi.service.impl;

import com.xinzhi.pojo.User;
import com.xinzhi.service.IVerifyService;
import com.xinzhi.service.IloggingStatusService;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class LoginStatusServiceImpl implements IloggingStatusService{
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IVerifyService verifyService;

    @Override
    public Result getLoggingStatus(String phone) {
        if (phone==null)
            return Result.error("手机号不能为空");
        if (!verifyService.isValidPhoneNumber(phone))
            return Result.error("手机号格式不正确");
        User u = (User) redisUtils.get("user" + phone);
        if (u==null)
            return Result.error("登录已过期，请重新登录");
        if (u.getUsername()==null) {
            redisUtils.set("user"+phone,u,300);
            return Result.ok(300, u);
        }
        redisUtils.set("user"+phone,u,300);
        return Result.ok(u);
    }

    @Override
    public Result deleteRedis(String phone) {
        if (phone==null)
            return Result.error("手机号不能为空");
        if (!verifyService.isValidPhoneNumber(phone))
            return Result.error("手机号格式不正确");
        Boolean delete = redisTemplate.delete("user" + phone);
        if (delete)
        return Result.ok("退出成功");
        return Result.error("退出失败");
    }
}
